//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using System.Security.Cryptography;
using Tathata.Common.Utils;
using System.Web.Security;
using Tathata.Common.BLL.Entities;
using Tathata.Common.Localization;
using Tathata.Common.BLL.EntityCollections;
using Tathata.Common.DAL.Managers;
using System.Web;

namespace Tathata.Common.BLL.Managers
{
    /// <summary>
    /// Customer role manager
    /// </summary>
    public class CustomerRoleManager
    {
        #region Constants
        private const string CUSTOMERROLES_ALL_KEY = "Nop.customerrole.all-{0}";
        private const string CUSTOMERROLES_BY_ID_KEY = "Nop.customerrole.id-{0}";
        #endregion

        #region Methods
        /// <summary>
        /// Marks customer role as deleted
        /// </summary>
        /// <param name="CustomerRoleID">Customer role identifier</param>
        public static void MarkCustomerRoleAsDeleted(int CustomerRoleID)
        {
            CustomerRole customerRole = GetByCustomerRoleID(CustomerRoleID);
            if (customerRole != null)
            {
                customerRole = UpdateCustomerRole(customerRole.CustomerRoleID, customerRole.Name,
                 customerRole.FreeShipping, customerRole.TaxExempt, customerRole.Active, true);
            }

            if (NopContext.IsCachable)
            {
                NopCache.RemoveByPattern(CUSTOMERROLES_ALL_KEY);
                NopCache.RemoveByPattern(CUSTOMERROLES_BY_ID_KEY);
            }
        }

        /// <summary>
        /// Gets a customer role
        /// </summary>
        /// <param name="CustomerRoleID">Customer role identifier</param>
        /// <returns>Customer role</returns>
        public static CustomerRole GetByCustomerRoleID(int CustomerRoleID)
        {
            string key = string.Format(CUSTOMERROLES_BY_ID_KEY, CustomerRoleID);
            object obj2 = NopCache.Get(key);
            if (NopContext.IsCachable && (obj2 != null))
            {
                return (CustomerRole)obj2;
            }

            CustomerRole customerRole = CustomerRoleDBManager.GetByCustomerRoleID(CustomerRoleID);

            if (NopContext.IsCachable)
            {
                NopCache.Max(key, customerRole);
            }
            return customerRole;
        }

        /// <summary>
        /// Gets all customer roles
        /// </summary>
        /// <returns>Customer role collection</returns>
        public static CustomerRoleCollection GetAllCustomerRoles()
        {
            bool showHidden = NopContext.Current.IsAdmin;
            string key = string.Format(CUSTOMERROLES_ALL_KEY, showHidden);
            object obj2 = NopCache.Get(key);
            if (NopContext.IsCachable && (obj2 != null))
            {
                return (CustomerRoleCollection)obj2;
            }

            CustomerRoleCollection customerRoleCollection = CustomerRoleDBManager.GetAllCustomerRoles(showHidden);
            if (NopContext.IsCachable)
            {
                NopCache.Max(key, customerRoleCollection);
            }
            return customerRoleCollection;
        }

        /// <summary>
        /// Gets customer roles by customer identifier
        /// </summary>
        /// <param name="CustomerID">Customer identifier</param>
        /// <returns>Customer role collection</returns>
        public static CustomerRoleCollection GetCustomerRolesByCustomerID(int CustomerID)
        {
            bool showHidden = NopContext.Current.IsAdmin;
            CustomerRoleCollection customerRoleCollection = CustomerRoleDBManager.GetCustomerRolesByCustomerID(CustomerID, showHidden);
            return customerRoleCollection;
        }

        /// <summary>
        /// Inserts a customer role
        /// </summary>
        /// <param name="Name">The customer role name</param>
        /// <param name="FreeShipping">A value indicating whether the customer role is marked as free shiping</param>
        /// <param name="TaxExempt">A value indicating whether the customer role is marked as tax exempt</param>
        /// <param name="Active">A value indicating whether the customer role is active</param>
        /// <param name="Deleted">A value indicating whether the customer role has been deleted</param>
        /// <returns>Customer role</returns>
        public static CustomerRole InsertCustomerRole(string Name, bool FreeShipping, bool TaxExempt,
            bool Active, bool Deleted)
        {
            CustomerRole customerRole = CustomerRoleDBManager.InsertCustomerRole(Name,
             FreeShipping, TaxExempt, Active, Deleted);

            if (NopContext.IsCachable)
            {
                NopCache.RemoveByPattern(CUSTOMERROLES_ALL_KEY);
                NopCache.RemoveByPattern(CUSTOMERROLES_BY_ID_KEY);
            }

            return customerRole;
        }

        /// <summary>
        /// Updates the customer role
        /// </summary>
        /// <param name="CustomerRoleID">The customer role identifier</param>
        /// <param name="Name">The customer role name</param>
        /// <param name="FreeShipping">A value indicating whether the customer role is marked as free shiping</param>
        /// <param name="TaxExempt">A value indicating whether the customer role is marked as tax exempt</param>
        /// <param name="Active">A value indicating whether the customer role is active</param>
        /// <param name="Deleted">A value indicating whether the customer role has been deleted</param>
        /// <returns>Customer role</returns>
        public static CustomerRole UpdateCustomerRole(int CustomerRoleID, string Name,
            bool FreeShipping, bool TaxExempt, bool Active, bool Deleted)
        {
            CustomerRole customerRole = CustomerRoleDBManager.UpdateCustomerRole(CustomerRoleID, Name,
              FreeShipping, TaxExempt, Active, Deleted);

            if (NopContext.IsCachable)
            {
                NopCache.RemoveByPattern(CUSTOMERROLES_ALL_KEY);
                NopCache.RemoveByPattern(CUSTOMERROLES_BY_ID_KEY);
            }

            return customerRole;
        }

        /// <summary>
        /// Adds a customer to role
        /// </summary>
        /// <param name="CustomerID">Customer identifier</param>
        /// <param name="CustomerRoleID">Customer role identifier</param>
        public static void AddCustomerToRole(int CustomerID, int CustomerRoleID)
        {
            CustomerRoleDBManager.AddCustomerToRole(CustomerID, CustomerRoleID);
        }

        /// <summary>
        /// Removes a customer from role
        /// </summary>
        /// <param name="CustomerID">Customer identifier</param>
        /// <param name="CustomerRoleID">Customer role identifier</param>
        public static void RemoveCustomerFromRole(int CustomerID, int CustomerRoleID)
        {
            CustomerRoleDBManager.RemoveCustomerFromRole(CustomerID, CustomerRoleID);
        }
        #endregion
    }
}
